//
//  ReceiveVC.m
//  CJRailTransport
//
//  Created by Hank on 2018/2/4.
//  Copyright © 2018年 amplity. All rights reserved.
//
#define SendLocationTitle          @"请选择送修地点"
#define SendTimeTitle              @"请选择送修日期"
#define MaintenancePartyTitle      @"请选择维修方"

#import "ReceiveVC.h"
#import "ReceiveCell.h"
#import "STPickerSingle.h"
#import "STPickerDate.h"
#import "UIButton+Expand.h"

@interface ReceiveVC ()<STPickerDateDelegate, STPickerSingleDelegate>
// 选择集装箱数组
@property (strong, nonatomic) NSMutableArray *containerDataArray;

@end

@implementation ReceiveVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏
    [self setNavC];
    // 设置界面属性
    [self setViewAttribute];
    // 注册Cell
    [self registerCells];
    // 设置BarButtonItem
    [self setBarButtonItem];
    // 设置圆角
    [self setRadius];
}

#pragma mark - 导航栏
- (void)setNavC
{
    self.title = @"接收";
    // 设置导航栏颜色
    BasicNavigationController *nVC = (BasicNavigationController *)self.navigationController;
    nVC.themeColor = [UIColor whiteColor];
    nVC.tintColor = [UIColor darkGrayColor];
}

// 设置界面属性
- (void)setViewAttribute
{
    self.view.backgroundColor = [UIColor whiteColor];
}

// 注册Cell
- (void)registerCells
{
    UINib *nib = [UINib nibWithNibName:[ReceiveCell reuseIdentifier] bundle:nil];
    [_myTableView registerNib:nib forCellReuseIdentifier:[ReceiveCell reuseIdentifier]];
}

// 设置BarButtonItem
- (void)setBarButtonItem
{
    UIButton *rightItemBtn = [UIButton setLeftOrRightButton:nil direction:@"right" title:@"提交"];
    [rightItemBtn addTarget:self action:@selector(rightItemAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightItemBtn];
}


#pragma mark - initArray
// 初始化数组
- (NSMutableArray *)containerDataArray
{
    if (!_containerDataArray) {
        _containerDataArray = [[NSMutableArray alloc]init];
    }
    return _containerDataArray;
}

#pragma mark - Radius
// 设置圆角
- (void)setRadius
{
    [self.sendLocation radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
    [self.sendTime radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
    [self.maintenanceParty radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
}

#pragma mark - Picker
// 显示日期选择器
- (void)showDatePicker
{
    STPickerDate *datePicker = [[STPickerDate alloc]init];
    [datePicker setDelegate:self];
    [datePicker setYearLeast:2000];
    [datePicker setYearSum:50];
    [datePicker setContentMode:STPickerContentModeBottom];
    [datePicker show];
}
// 显示内容选择器
- (void)showContentPicker:(NSMutableArray *)dataArray pickerTitle:(NSString *)pickerTitle
{
    // 创建内容选择器
    STPickerSingle *pickerSingle = [[STPickerSingle alloc]init];
    // 添加数据
    [pickerSingle setArrayData:dataArray];
    // 添加标题
    [pickerSingle setTitle:pickerTitle];
    [pickerSingle setContentMode:STPickerContentModeBottom];
    [pickerSingle setDelegate:self];
    // 显示内容选择器
    [pickerSingle show];
}

#pragma mark - Action
// 提交 Action
- (void)rightItemAction
{
    
}

#pragma mark - Action
// 添加集装箱
- (IBAction)addContainerAction:(UIButton *)sender
{
    [_containerDataArray addObject:@""];
    [_myTableView reloadData];
}
// 送修地点 Action
- (IBAction)sendLocationAction:(UIButton *)sender
{
    // 显示内容选择器
    [self showContentPicker:[NSMutableArray arrayWithArray:@[@"送修地点测试1",@"送修地点测试2",@"送修地点测试3",@"送修地点测试4",@"送修地点测试5",@"送修地点测试6"]] pickerTitle:SendLocationTitle];
}
// 送修日期 Action
- (IBAction)sendTimeAction:(UIButton *)sender
{
    // 显示日期选择器
    [self showDatePicker];
}
// 维修方 Action
- (IBAction)maintenancePartyAction:(UIButton *)sender
{
    // 显示内容选择器
    [self showContentPicker:[NSMutableArray arrayWithArray:@[@"维修方测试1",@"维修方测试2",@"维修方测试3",@"维修方测试4",@"维修方测试5",@"维修方测试6"]] pickerTitle:MaintenancePartyTitle];
}

#pragma mark - pickerDateDelegate
// date
-(void)pickerDate:(STPickerDate *)pickerDate year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    // 更新日期
    [self.sendTime setTitle:[NSString stringWithFormat:@"  %ld-%@-%@", year, [self digitJudge:month], [self digitJudge:day]] forState:UIControlStateNormal];
}
// 数字位数判断
- (NSString *)digitJudge:(NSInteger)number
{
    // 转换的字符串
    NSString *numberStr;
    
    if (number < 10) {
        numberStr = [NSString stringWithFormat:@"0%ld",number];
    }else{
        numberStr = [NSString stringWithFormat:@"%ld",number];
    }
    return numberStr;
}

// content
- (void)pickerSingle:(STPickerSingle *)pickerSingle selectedTitle:(NSString *)selectedTitle
{
    if ([pickerSingle.title isEqualToString:SendLocationTitle]) {
        [self.sendLocation setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
    }else if ([pickerSingle.title isEqualToString:SendTimeTitle]) {
        [self.sendTime setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
    }else if ([pickerSingle.title isEqualToString:MaintenancePartyTitle]) {
        [self.maintenanceParty setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
    }
}

#pragma mark - TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.containerDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReceiveCell *cell = [tableView dequeueReusableCellWithIdentifier:[ReceiveCell reuseIdentifier] forIndexPath:indexPath];
    cell.indexPath = indexPath;
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 移除数据
        [_containerDataArray removeObjectAtIndex:indexPath.row];
        // 移除单元格
        //        [_myTableView reloadData];
        [_myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

// 设置进入编辑状态时，Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

#pragma mark - TableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
